package com.soulkun.smarthome.ui.main.personal

import androidx.databinding.ObservableArrayList
import androidx.fragment.app.viewModels
import androidx.recyclerview.widget.LinearLayoutManager
import com.soulkun.smarthome.R
import com.soulkun.smarthome.databinding.AppFragmentMainPersonalMessageAlertBinding
import com.soulkun.smarthome.logic.dto.AlertMessageDTO
import com.soulkun.smarthome.logic.enum.MessageCodeEnum
import com.soulkun.smarthome.logic.manager.BroadcastManager
import com.soulkun.smarthome.logic.vo.AlertMessageHistoryItemVO
import com.soulkun.smarthome.logic.vo.AlertMessageHistoryVO
import com.soulkun.smarthome.ui.main.personal.adapter.AppMainPersonalMessageAlertContainerListAdapter
import com.soulkun.smarthome.ui.main.personal.viewmodel.AppMainPersonalMessageAlertViewModel
import soulkun.library.helper.decoration.LinearSpaceItemDecorationHelper
import soulkun.library.mvvm.component.BaseDataBindingFragment
import soulkun.library.utils.time.DateUtils
import soulkun.library.utils.time.TimeUtils

class AppMainPersonalMessageAlertFragment :
    BaseDataBindingFragment<AppFragmentMainPersonalMessageAlertBinding>(false) {

    private val viewModel by viewModels<AppMainPersonalMessageAlertViewModel> { defaultViewModelProviderFactory }

    private val adapter by lazy {
        AppMainPersonalMessageAlertContainerListAdapter(mContext, viewModel.alertMessageList)
    }

    private val alertBroadcastReceiver by lazy {
        BroadcastManager.SoulkunBroadcastReceiver(MessageCodeEnum.Alert) {
            val alertMessageDTO = it as AlertMessageDTO
            val messageDate = DateUtils.stringToDate(alertMessageDTO.at).time
            viewModel.alertMessageList.find { alertMessageHistoryVO -> alertMessageHistoryVO.dateTime == messageDate }
                ?.let { alertMessageHistoryVO ->
                    alertMessageHistoryVO.messageList.add(
                        0, AlertMessageHistoryItemVO(
                            "家庭“${alertMessageDTO.familyName}”中设备“${alertMessageDTO.deviceName}”的属性“${alertMessageDTO.attributeDescription}”${alertMessageDTO.content}",
                            TimeUtils.stringToTime(alertMessageDTO.at, "yyyy-MM-dd HH:mm:ss.SSS")
                                ?: ""
                        )
                    )
                    return@SoulkunBroadcastReceiver
                }
            viewModel.alertMessageList.add(
                0,
                AlertMessageHistoryVO(
                    messageDate,
                    ObservableArrayList<AlertMessageHistoryItemVO>().apply {
                        add(
                            AlertMessageHistoryItemVO(
                                "家庭“${alertMessageDTO.familyName}”中设备“${alertMessageDTO.deviceName}”的属性“${alertMessageDTO.attributeDescription}”${alertMessageDTO.content}",
                                TimeUtils.stringToTime(
                                    alertMessageDTO.at,
                                    "yyyy-MM-dd HH:mm:ss.SSS"
                                )
                                    ?: ""
                            )
                        )
                    })
            )
        }
    }

    override fun setLayout(): Int {
        return R.layout.app_fragment_main_personal_message_alert
    }

    override fun initView() {
        requireDataBinding().rvMainPersonalMessageAlertListContainer.run {
            layoutManager = LinearLayoutManager(mContext)
            adapter = this@AppMainPersonalMessageAlertFragment.adapter
            if (itemDecorationCount == 0) {
                addItemDecoration(LinearSpaceItemDecorationHelper(10, 10, 15, 15, 10))
            }
        }
    }

    override fun initData() {
        viewModel.getAlertMessageList()
    }

    override fun initListener() {
        BroadcastManager.registerBroadcast(mContext, MessageCodeEnum.Alert, alertBroadcastReceiver)
    }

    override fun doOnCreate() {}

    override fun onDestroyView() {
        super.onDestroyView()
        BroadcastManager.unregisterBroadcast(mContext, alertBroadcastReceiver)
    }

}